      1  import java.awt.BorderLayout;

      2  import java.awt.GridLayout;

      3  import java.awt.event.ActionEvent;

      4  import java.awt.event.ActionListener;

      5  import javax.swing.JButton;

      6  import javax.swing.JPanel;

      7  import javax.swing.JTextField;

      8

      9  /**

     10     A component that lets the user enter a number, using

     11     a button pad labeled with digits.

     12  */

     13  public class KeyPad extends JPanel

     14  {

     15     private JPanel buttonPanel;

     16     private JButton clearButton;

     17     private JTextField display;

     18

     19     /**

     20        Constructs the keypad panel.

     21     */

     22     public KeyPad()

     23     {

     24        setLayout(new BorderLayout());

     25

     26        // Add display field

     27

     28        display = new JTextField();

     29        add(display, "North");

     30

     31        // Make button panel

     32

     33        buttonPanel = new JPanel();

     34        buttonPanel.setLayout(new GridLayout(4, 3));

     35

     36        // Add digit buttons

     37

     38        addButton("7");

     39        addButton("8");

     40        addButton("9");

     41        addButton("4");

     42        addButton("5");

     43        addButton("6");

     44        addButton("1");

     45        addButton("2");

     46        addButton("3");

     47        addButton("0");

     48        addButton(".");

     49

     50        // Add clear entry button

     51

     52        clearButton = new JButton("CE");

     53        buttonPanel.add(clearButton);

     54

     55        class ClearButtonListener implements ActionListener

     56        {

     57           public void actionPerformed(ActionEvent event)

     58           {

     59              display.setText("");

     60           }

     61        }

     62

     63        ActionListener listener = new ClearButtonListener();

     64

     65        clearButton.addActionListener(new

     66              ClearButtonListener());

     67

     68        add(buttonPanel, "Center");

     69     }

     70

     71     /**

     72        Adds a button to the button panel

     73        @param label the button label

     74     */

     75     private void addButton(final String label)

     76     {

     77        class DigitButtonListener implements ActionListener

     78        {

     79           public void actionPerformed(ActionEvent event)

     80           {

     81              // Don't add two decimal points

     82              if (label.equals(".")

     83                    && display.getText().indexOf(".") != -1)

     84              {

     85                 return;

     86              }

     87

     88              // Append label text to button

     89              display.setText(display.getText() + label);

     90           }

     91        }

     92

     93        JButton button = new JButton(label);

     94        buttonPanel.add(button);

     95        ActionListener listener = new DigitButtonListener();

     96        button.addActionListener(listener);

     97     }

     98

     99     /**

    100        Gets the value that the user entered.

    101        @return the value in the text field of the keypad

    102     */

    103     public double getValue()

    104     {

    105        return Double.parseDouble(display.getText());

    106     }

    107

    108     /**

    109        Clears the display.

    110     */

    111     public void clear()

    112     {

    113        display.setText("");

    114     }

    115  }

    116